package com.tianhui.consignortwofei.mvp.ui.activity.hetong.activity;

import android.text.TextUtils;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.hetong.AuthorizedRepresentativeInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.CompanyInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractInfo;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2021/5/19
 * @description
 **/
public class ContractAddStepThreeActivity extends BaseContractAddActivity {
    @BindView(R.id.activity_contract_add_step_three_companyNameFirstPartyInputItemView)
    protected InputItemView mCompanyNameFirstPartyInputItemView;
    @BindView(R.id.activity_contract_add_step_three_companyAddressFirstPartyInputItemView)
    protected InputItemView mCompanyAddressFirstPartyInputItemView;
    @BindView(R.id.activity_contract_add_step_three_legalPersonFirstPartyInputItemView)
    protected InputItemView mLegalPersonFirstPartyInputItemView;

    @BindView(R.id.activity_contract_add_step_three_authorizedRepresentativeFirstPartyInputItemView)
    protected InputItemView mAuthorizedRepresentativeFirstPartyInputItemView;

    @BindView(R.id.activity_contract_add_step_three_phoneFirstPartyInputItemView)
    protected InputItemView mPhoneFirstPartyInputItemView;
    @BindView(R.id.activity_contract_add_step_three_faxFirstPartyInputItemView)
    protected InputItemView mFaxFirstPartyInputItemView;

    @BindView(R.id.activity_contract_add_step_three_companyNamePartyBInputItemView)
    protected InputItemView mCompanyNamePartyBInputItemView;
    @BindView(R.id.activity_contract_add_step_three_companyAddressPartyBInputItemView)
    protected InputItemView mCompanyAddressPartyBInputItemView;
    @BindView(R.id.activity_contract_add_step_three_legalPersonPartyBInputItemView)
    protected InputItemView mLegalPersonPartyBInputItemView;

    @BindView(R.id.activity_contract_add_step_three_authorizedRepresentativePartyBInputItemView)
    protected InputItemView mAuthorizedRepresentativePartyBInputItemView;
    @BindView(R.id.activity_contract_add_step_three_phonePartyBInputItemView)
    protected InputItemView mPhonePartyBInputItemView;
    @BindView(R.id.activity_contract_add_step_three_faxPartyBInputItemView)
    protected InputItemView mFaxPartyBInputItemView;

    private SelectDialog mAuthorizedRepresentativeFirstPartySelectDialog;
    private SelectDialog mCompanyPartyBSelectDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_contract_add_step_three;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public ContractInfo getSaveInfo() {
        String companyNameFirstParty = mCompanyNameFirstPartyInputItemView.getContent();
        if (TextUtils.isEmpty(companyNameFirstParty)) {
            showToast("请输入公司名称（甲方）");
            return null;
        }
        mContractInfo.company_a = companyNameFirstParty;

        String companyAddressFirstParty = mCompanyAddressFirstPartyInputItemView.getContent();
        if (TextUtils.isEmpty(companyAddressFirstParty)) {
            showToast("请输入公司地址（甲方）");
            return null;
        }
        mContractInfo.address_a = companyAddressFirstParty;

        String legalPersonFirstParty = mLegalPersonFirstPartyInputItemView.getContent();
        if (TextUtils.isEmpty(legalPersonFirstParty)) {
            showToast("请输入公司法人（甲方）");
            return null;
        }
        mContractInfo.legalperson_a = legalPersonFirstParty;

        String faxFirstParty = mFaxFirstPartyInputItemView.getContent();
        mContractInfo.fax_a = faxFirstParty;


        String authRepresentativeFirstParty = mAuthorizedRepresentativeFirstPartyInputItemView.getContent();
        if (TextUtils.isEmpty(authRepresentativeFirstParty)) {
            showToast("请选择授权代表（甲方）");
            return null;
        }
        mContractInfo.representative_a = authRepresentativeFirstParty;

        String phoneFirstParty = mPhoneFirstPartyInputItemView.getContent();
        if (TextUtils.isEmpty(phoneFirstParty)) {
            showToast("请输入联系电话（甲方）");
            return null;
        }
        mContractInfo.tel_a = phoneFirstParty;


        String companyNamePartyB = mCompanyNamePartyBInputItemView.getContent();
        if (TextUtils.isEmpty(companyNamePartyB)) {
            showToast("请选择公司名称（乙方）");
            return null;
        }
        mContractInfo.company_b = companyNamePartyB;
        String companyAddressPartyB = mCompanyAddressPartyBInputItemView.getContent();
        if (TextUtils.isEmpty(companyAddressPartyB)) {
            showToast("请输入公司地址（乙方）");
            return null;
        }
        mContractInfo.address_ = companyAddressPartyB;

        String legalPersonPartyB = mLegalPersonPartyBInputItemView.getContent();
        if (TextUtils.isEmpty(legalPersonPartyB)) {
            showToast("请输入公司法人（乙方）");
            return null;
        }
        mContractInfo.legalperson_b = legalPersonPartyB;

        String authRepresentativePartyB = mAuthorizedRepresentativePartyBInputItemView.getContent();
        if (TextUtils.isEmpty(authRepresentativePartyB)) {
            showToast("请选择授权代表（乙方）");
            return null;
        }
        mContractInfo.representative_b = authRepresentativePartyB;

        String phonePartyB = mPhonePartyBInputItemView.getContent();
        if (TextUtils.isEmpty(phonePartyB)) {
            showToast("请输入联系电话（乙方）");
            return null;
        }
        mContractInfo.tel_b = phonePartyB;

        String faxPartyB = mFaxPartyBInputItemView.getContent();
        mContractInfo.fax_b = faxPartyB;

        return mContractInfo;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        mCompanyNameFirstPartyInputItemView.setContent(mContractInfo.company_a);
        mCompanyAddressFirstPartyInputItemView.setContent(mContractInfo.address_a);
        mLegalPersonFirstPartyInputItemView.setContent(mContractInfo.legalperson_a);
    }

    @Override
    public void initListener() {

    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_contract_add_step_three_authorizedRepresentativeFirstPartyFrameLayout)
    public void selectAuthorizedRepresentativeFirstParty() {
        if (mAuthorizedRepresentativeFirstPartySelectDialog == null) {
            requestAuthAuthorizedRepresentativeFirstPartyList();
        } else {
            mAuthorizedRepresentativeFirstPartySelectDialog.show();
        }
    }

    private void requestAuthAuthorizedRepresentativeFirstPartyList() {
        String id = SharedPreferenceManager.getInstance().getConsignorId();
        Map<String, String> map = new HashMap<>();
        map.put("createby", id);
        mContractModel.authorizedRepresentativeListModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<AuthorizedRepresentativeInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<AuthorizedRepresentativeInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<AuthorizedRepresentativeInfo> list = commonResponse.getData();
                    if (list != null && list.size() > 0) {
                        showAuthorizedRepresentativeFirstPartyDialog(list);
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    @OnClick(R.id.activity_contract_add_step_three_companyNamePartyBFrameLayout)
    public void selectCompanyPartyB() {
        if (mCompanyPartyBSelectDialog == null) {
            requestCompanyPartBList();
        } else {
            mCompanyPartyBSelectDialog.show();
        }
    }

    private void requestCompanyPartBList() {
        String id = SharedPreferenceManager.getInstance().getConsignorId();
        Map<String, String> map = new HashMap<>();
        map.put("createby", id);
        mContractModel.companyPartyBListModel(getContext(), map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<CompanyInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<CompanyInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<CompanyInfo> companyInfoList = commonResponse.getData();
                    if (companyInfoList != null && companyInfoList.size() > 0) {
                        showCompanyPartyBDialog(companyInfoList);
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    private void showAuthorizedRepresentativeFirstPartyDialog(List<AuthorizedRepresentativeInfo> list) {
        if (mAuthorizedRepresentativeFirstPartySelectDialog == null) {

            mAuthorizedRepresentativeFirstPartySelectDialog = new SelectDialog<AuthorizedRepresentativeInfo>(this);
            mAuthorizedRepresentativeFirstPartySelectDialog.setTitle("授权代表(甲方)");
            mAuthorizedRepresentativeFirstPartySelectDialog.setList(list);
            mAuthorizedRepresentativeFirstPartySelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<AuthorizedRepresentativeInfo>() {
                @Override
                public void onItemClick(AuthorizedRepresentativeInfo representativeInfo, int position) {
                    mAuthorizedRepresentativeFirstPartyInputItemView.setContent(representativeInfo.getSelectItemText());
                    mPhoneFirstPartyInputItemView.setContent(representativeInfo.tel_a);

                }
            });
        }
        mAuthorizedRepresentativeFirstPartySelectDialog.show();


    }

    private void showCompanyPartyBDialog(List<CompanyInfo> companyInfoList) {
        if (mCompanyPartyBSelectDialog == null) {

            mCompanyPartyBSelectDialog = new SelectDialog<CompanyInfo>(this);
            mCompanyPartyBSelectDialog.setTitle("公司名称(乙方)");
            mCompanyPartyBSelectDialog.setList(companyInfoList);
            mCompanyPartyBSelectDialog.setOnSelectItemClickLister(new SelectDialog.OnSelectItemClickListener<CompanyInfo>() {
                @Override
                public void onItemClick(CompanyInfo companyInfo, int position) {
                    mCompanyNamePartyBInputItemView.setContent(companyInfo.getSelectItemText());

                    mCompanyAddressPartyBInputItemView.setContent(companyInfo.company_b);
                    mLegalPersonPartyBInputItemView.setContent(companyInfo.legalperson_b);
                    mAuthorizedRepresentativePartyBInputItemView.setContent(companyInfo.representative_b);
                    mPhonePartyBInputItemView.setContent(companyInfo.tel_b);
                    mFaxPartyBInputItemView.setContent(companyInfo.fax_b);

                }
            });
        }
        mCompanyPartyBSelectDialog.show();


    }


    @OnClick(R.id.activity_contract_add_step_three_nextButton)
    public void next() {
        ContractInfo contractInfo = getSaveInfo();
        if (contractInfo != null) {
            openNextActivity(contractInfo);
        }
    }

    protected void openNextActivity(ContractInfo contractInfo) {
        openNextStepActivity(contractInfo, ContractAddStepFourActivity.class);
    }

}
